//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    indexArray:[],
    showCardValue:''
  },

  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },

  onLoad: function () {
    this.setData({
      indexArray: [0,0,0,0,0,0,0,0,0,0,0,0,0,0],
      showCardValue: ''
    })    
  },

  /**
     * 生命周期函数--监听页面初次渲染完成
     */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.onLoad();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }, 

  // 计算小时
  calcWorkTime: function(){
    
    var idxArray = this.data.indexArray;

    var workTimeShow = [0, 0.5, 1, 2, 3, 5, 8, 13, 20, 40, 100, 'Big', '?', '♨'];
    var workTimeResult = ['小时', '小时', '小时', '小时', '小时', '小时', '小时', '小时', '小时', '小时', '小时', '太大', '不懂', '休息'];
   

    var totalTimeShow = '';
    var totalTimeResult = '';
    for (var i = 0, j = 0; i < idxArray.length; i++) {
      
      if (i < 11 && idxArray[i] == 1) {
        j += workTimeShow[i];
        totalTimeShow = j;
        totalTimeResult = workTimeResult[i];
      } else if (i > 10 && idxArray[i] == 1) {
        totalTimeShow = workTimeShow[i];
        totalTimeResult = workTimeResult[i];
      }

    
    }


    app.globalData.finalResult = { "totalTimeShow": totalTimeShow, "totalTimeResult": totalTimeResult }

    },

  // 状态变更及赋值
  changeStatus: function(index){
    
    this.data.indexArray[index] = (this.data.indexArray[index] == 0) ? 1 : 0;

    if (index < 11 && this.data.indexArray[index] == 1) {
      this.data.indexArray.splice(11, 3, 0, 0, 0);
      this.data.indexArray[index] = 1;
    }

    if (index > 10 && this.data.indexArray[index] == 1) {
      this.data.indexArray.splice(0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
      this.data.indexArray[index] = 1;
    }
    
    this.setData({
      indexArray: this.data.indexArray
    })

    this.calcWorkTime();

  },


  toast:function(event){
    var index = Number(event.target.dataset.index);
    switch (index) {
      case 0:
        this.changeStatus(index);
        
        break;
      case 1:
        this.changeStatus(index);

      break;
      case 2:
        this.changeStatus(index);
      break;
      case 3:
        this.changeStatus(index);
      break;
      case 4:
        this.changeStatus(index);
      break;
      case 5:
        this.changeStatus(index);
      break;
      case 6:
        this.changeStatus(index);
      break;
      case 7:
        this.changeStatus(index);
      break;             
      case 8:
        this.changeStatus(index);
      break;  
      case 9:
        this.changeStatus(index);
      break;
      case 10:
        this.changeStatus(index);
      break;
      case 11:
        this.changeStatus(index);        
      break;
      case 12:
        this.changeStatus(index);
      break;
      case 13:
        this.changeStatus(index);
      break;
    }

    var showCardVal = app.globalData.finalResult.totalTimeShow + app.globalData.finalResult.totalTimeResult;

    this.setData({
      showCardValue: showCardVal ? '(' + showCardVal + ')':''
    })
  
    // console.log(this.data.indexArray);
  },
  // 出牌
  showCard : function(event) {

    if (this.data.showCardValue != '') {
      wx.navigateTo({
        url: '../index/waitopen'
      })
    } else {
      wx.showToast({
        title: '请先选择工时',
        icon: 'none',
        duration: 1000
      })

      // wx.showModal({
      //   title: '提示',
      //   content: '这是一个模态弹窗',
      //   success: function (res) {
      //     if (res.confirm) {
      //       console.log('用户点击确定')
      //     } else if (res.cancel) {
      //       console.log('用户点击取消')
      //     }
      //   }
      // })

      // wx.showActionSheet({
      //   itemList: ['A', 'B', 'C'],
      //   success: function (res) {
      //     console.log(res.tapIndex)
      //   },
      //   fail: function (res) {
      //     console.log(res.errMsg)
      //   }
      // })


      
    }
   
  },
  // 清理
  clearCard: function(event) {
    this.onLoad();
  }


})
